<?php

/**
 * 统计信息logic
 */

namespace Wechat\Logic;

class AnalyseLogic extends CommonLogic {
	
	const USERADD = 'useradd';//新增微信收听用户
	const USERDEL = 'userdel';//减去用户新增用户
	const WEBSITE_VIEW = 'website_view';//微官网访问人数
	const MALL_VIEW = 'mall_view';//微商城访问人数
	const FROM_LINK = 'from_link';//来源统计
	const SCTATCH_VIEW='sctatch_view';//刮刮卡
	const FRUIT_VIEW='fruit_view';//水果达人
	const TURNTABLE_VIEW='turntable_view';//欢乐大转盘
	const MENU_CLICK='menu_click';//自定义菜单设置
	
	
	public function getTypeName($type){
		if($type == self::USERADD){
			return '每天新增粉丝数';
		}else if($type == self::USERDEL){
			return '每天流失粉丝数';
		}else if($type == self::WEBSITE_VIEW){
			return '微官网每天访问人数';
		}else if($type == self::MALL_VIEW){
			return '微商城每天访问人数';
		}else if($type == self::FROM_LINK){
			return '来源统计';
		}else if($type==self::SCTATCH_VIEW){
			return'刮刮卡';
		}else if($type==self::FRUIT_VIEW){
			return '水果达人';
		}else if($type==self::TURNTABLE_VIEW){
			return '欢乐大转盘';
		}else if ($type==self::MENU_CLICK){
			return'自定义菜单点击量';
		}
	}
	
	/**
	 * 添加记录
	 * @param unknown $uid
	 * @param unknown $type
	 * @param unknown $time
	 * @return Ambigous <\Think\mixed, boolean, string, unknown>|Ambigous <boolean, unknown>
	 */
	public function logadd($uid,$type,$time){
		
		$map = array(
		    'uid'=>$uid,
			'type'=>$type,
			'time'=>(strtotime(date('Y-m-d',$time))+1)
		);
		
		$res = $this->where($map)->find();
		
		if(empty($res)){
			$data = $map;
		    $data['count'] = 1;
		    return  $this->add($data);
		}else{
			$data = $res;
			$data['count'] += 1;
			return  $this->where($map)->save($data);
		}
		
	    
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::update()
	 */
	public function update($data = array(),$map = array('uid'=>UID)){
		
		if(empty($data)){
			$data = $this->create();
		}
		
		if($data === false){
			return false;
		}
		
		$info = $this->isexists($map);
		
		/* 添加或更新数据 */
		if($info === false){//新增数据
			$data['uid'] = UID;
			$id = $this->add($data);
			if(!$id){
				$this->error = '新增详细内容失败！';
				return false;
			}
			return $id;
			
		} else { //更新数据
			$status = $this->where(array('id'=>$info['id']))->save($data);
			if(false === $status){
				$this->error = '更新详细内容失败！';
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::autoSave()
	 */
	public function autoSave($id = 0){
		 
	}
}